import {defineStore} from "pinia";
import {ref,computed} from "vue";

interface UserType{
    account:string
    avatar:string
    birthday:string
    cityCode:string
    gender:string
    id:string
    mobile:string
    nickname:string
    profession:string
    provinceCode:string
    token:string
}
const useUserStore = defineStore('user', () => {
    const userInfo = ref<UserType>();
    const getUser = ()=>{
        const local = localStorage.getItem('xtxUser')
        if(local) userInfo.value = JSON.parse(local)
        return userInfo.value
    }
    const setUser = (user:UserType)=>{
        userInfo.value = user
        if (userInfo.value?.token){
            localStorage.setItem('xtxUser',JSON.stringify(userInfo.value))
        }
    }

    const logout = ()=>{
        userInfo.value = undefined
        localStorage.removeItem('xtxUser')
    }
    return{
        userInfo,
        setUser,
        logout,
        getUser
    }
})
export default useUserStore